package Servlet_Four;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.staff.Util.JDBC_Staff;
import com.staff.model.emoplyee;

@WebServlet("/EmployeeServlet")
public class EmployeeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public EmployeeServlet() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String category = request.getParameter("category");
		if("update".equals(category)) {	
			int   id = Integer.valueOf(request.getParameter("id"));
			String name = request.getParameter("name");
			String sex = request.getParameter("sex");
			int  age=Integer.valueOf(request.getParameter("age"));
			String dapartment = request.getParameter("dapartment");
			String type = request.getParameter("type");
			int year = Integer.valueOf(request.getParameter("year"));
			Object[] obs = {name,sex,age,dapartment,type,year,id};
			String sql = "update employee set name=?,sex=?,age=?,dapartment=?,type=?,year=? where id=?";
			JDBC_Staff.excumentDBUpdate(sql, obs);
		}
		else if("remove".equals(category)) {
			int   id = Integer.valueOf(request.getParameter("id"));
			Object[] obs = {id};
			String sql = "DELETE FROM employee WHERE id = ?";
			JDBC_Staff.excumentDBUpdate(sql, obs);
		}
		else if("add".equals(category)) {
			String sql1 = "select * from  employee order by id desc limit 1";
			Object[] obs1 = {};
			emoplyee list = new  emoplyee();
			list = JDBC_Staff.getSingleResult(sql1, obs1, emoplyee.class);
			int id = list.getId()+1;
	        String name = request.getParameter("name");
			String sex = request.getParameter("sex");
			int  age=Integer.valueOf(request.getParameter("age"));
			String dapartment = request.getParameter("dapartment");
			String type = request.getParameter("type");
			int year = Integer.valueOf(request.getParameter("year"));
			Object[] obs2 = {id,name,sex,age,dapartment,type,year};
			String sql2 = "INSERT INTO employee VALUES(?,?,?,?,?,?,?)";
			JDBC_Staff.excumentDBUpdate(sql2, obs2);
		}
		response.sendRedirect("/Java_web/showEmployee_Four.jsp");
	}

}
